import axios from 'axios';
import qs from 'qs';
const config = require('../../../config');
const baseUrl = !!process.env  ? config.dev.proxypath : config.build.proxypath;

export default function fetch(options) {
    if(options.method == "post"){
      options.data = qs.stringify(options.data);
    }
    return new Promise((resolve, reject) => {
        const instance = axios.create({
            // timeout: 2000,
            // headers: { 'X-Ivanka-Token': store.getters.token }
            withCredentials: true,
            headers: {
                // 'Content-Type':'application/x-www-form-urlencoded',
                'X-Requested-With': 'XMLHttpRequest'
            }
        });
        instance(options)
            .then(response => {
                const res = response.data;
                if(res.code == "-1") {
                    window.location.href = "./login.html";
                    return;
                }else if(res.code != "1"){
                  reject(res.msg);
                }else{
                  resolve(res);
                }
            })
            .catch(error => {
                reject(error);
            });
    });
}
